package com.mystudy.management.usermanagement.controller;


import com.alibaba.fastjson.JSONObject;
import com.mystudy.management.usermanagement.dao.pojo.Visitor;
import com.mystudy.management.usermanagement.laytablemessageconvert.LayuiSelectMessageConvert;
import com.mystudy.management.usermanagement.laytablemessageconvert.LayuiTableMessageConvert;
import com.mystudy.management.usermanagement.service.Index2_2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin/index/supervision/control")
public class Index2_2Controller {


    @Autowired
    private Index2_2Service service;//注入服务

    @Autowired
    private LayuiTableMessageConvert tableMessageConvert;//注入返回信息转换工具

    @Autowired
    private LayuiSelectMessageConvert selectMessageConvert;//输入请求解析工具


    /*
    -----------------------------------------受控表--------------------------------------------
     */
    //返回所有受控信息
    @GetMapping("/controlvisitors")
    public JSONObject getControlVisitorsTable(){
        return (JSONObject) tableMessageConvert.index2_2messagePacking(service.getAllLimits());
    }

    //查找受控信息
    @GetMapping("/controlvisitors/{indexkind}/{message}/{enddate}")
    public JSONObject getControlVisitorByConditions(@PathVariable("indexkind") String indexKind,
                                                    @PathVariable("message") String message,
                                                    @PathVariable("enddate") String endDate) throws ParseException {
        if(indexKind.equals("byName")){
            return (JSONObject)tableMessageConvert.index2_2messagePacking(service.getLimitsByCondition((String)selectMessageConvert.index2_2SelectMessageConvert(message).get("userName")));
        }else if(indexKind.equals("byId")){
            return (JSONObject)tableMessageConvert.index2_2messagePacking(service.getLimitsByCondition((Integer) selectMessageConvert.index2_2SelectMessageConvert(Integer.valueOf(message)).get("userId")));
        }else {
            return (JSONObject)tableMessageConvert.index2_2messagePacking(service.getLimitsByCondition((Date) selectMessageConvert.index2_2SelectMessageConvert(message,endDate).get("startDate"),
                    (Date) selectMessageConvert.index2_2SelectMessageConvert(message,endDate).get("endDate")));
        }

    }

    //批量删除受控信息
    @DeleteMapping("/controlvisitors")
    public String deleteControlDataByIds(@RequestBody JSONObject requestBody){
       Integer[] deleteIds = this.selectMessageConvert.index2_2DeleteMessageConvert(requestBody);
        Map<String,Integer[]> resultResponse = service.deleteLimitsByVisitorIds(deleteIds);
        Integer[] failed = resultResponse.get("Failed");
        if(failed.length != 0 ){
            return "存在"+failed.length+"条数据删除失败！";
        }
        return "删除成功！";
    }

    //批量修改控制信息
    @PostMapping("/controlvisitors")
    public String modifyControlDataByIds(@RequestBody JSONObject requestBody){
        Map<Integer,Date[]> requestMessage = null;
        try {
            requestMessage = this.selectMessageConvert.index2_2ModifyMessageConvert(requestBody);
            service.updateLimitsByVisitorIds(requestMessage);
        } catch (ParseException e) {
            e.printStackTrace();
            return "修改失败！";
        }
        return "修改成功！";
    }

    /*
    -----------------------------------------------自由表---------------------------------------------------------
     */

    //获取所有自由用户
    @GetMapping("/uncontrolvisitors")
    public JSONObject getAllUnControlVisitors(){
        return (JSONObject) tableMessageConvert.index2_2messagePacking(service.getAllUnLimitVisitors());
    }

    //根据索引查询自由用户
    @GetMapping("/uncontrolvisitors/{indexkind}/{index}")
    public JSONObject getUnControlVisitorByIndex(@PathVariable("indexkind") String indexKind,
                                                 @PathVariable("index") String index){
        Map<String,Object> requestMessage = selectMessageConvert.index2_2SelectUnControlMessageConvert(indexKind,index);
        List<Visitor> resultPremitive ;
        if(indexKind.equals("byName")){
            resultPremitive =  service.getUnLimitVisitorByIndex((String) requestMessage.get("userName"));
        }else{
            resultPremitive = service.getUnLimitVisitorByIndex((Integer) requestMessage.get("userId"));
        }

        return (JSONObject) tableMessageConvert.index2_2messagePacking(resultPremitive);
    }

    //添加控制
    @PutMapping("/uncontrolvisitors")
    public String addNewControlVisitors(@RequestBody JSONObject requestBody){
        try {
            Map<Integer,Date[]> requestMessage = selectMessageConvert.index2_2AddMessageConvert(requestBody);
            Integer[] resultCode = service.addLimitsByVisitorIds(requestMessage);
            return "成功添加"+resultCode[0]+"个，失败"+resultCode[1]+"个";
        }catch (ParseException e){
            e.printStackTrace();
            return "全部添加失败！";
        }

    }
}
